<?php
class Nilai_model extends MY_Model {
	
    function Nilai_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "nilai";
        $this->table_fields = array('nilai_id','nilai_point','nilai_min', 'nilai_max', 'detachiev_id', 'reward_id');
    }
    
    /**
     * @deprecated
     * @param $search_key
     * @param $where
     * @param $limit
     * @param $offset
     * @param $orderby
     */
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    	$db = $this->db;
    	$db->select('*');
		$db->join('prestasi_pelanggaran_detil', 'prestasi_pelanggaran_detil.detachiev_id = '.$this->table_name.'.detachiev_id');
		$db->join('prestasi_pelanggaran', 'prestasi_pelanggaran.achiev_id = prestasi_pelanggaran_detil.achiev_id');
		$db->join('reward_punishment', 'reward_punishment.reward_id = '.$this->table_name.'.reward_id');
		
		if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "").$this->table_name.".$ff LIKE ". $this->db->escape("%$kk%");
				}
				
				foreach(array('achiev_jenis','achiev_bidang','detachiev_nama','detachiev_tingkat', 'reward_nama', 'reward_jenis') as $ff){
					$qq .= ($qq? " OR " : "")."$ff LIKE ". $this->db->escape("%$kk%"); 
				}
			}
			$db->where("($qq)");
		}
		
		if ($where) $db->where($where);
		
    	if ($orderby) $db->order_by($orderby);
		
		$db->limit($limit, $offset);
		
		return $db->get($this->table_name);
    }
        
    function select_by_id($id, $orderby=false){
    	$db = $this->db;
    	$db->select('*');
		$db->join('prestasi_pelanggaran_detil', 'prestasi_pelanggaran_detil.detachiev_id = '.$this->table_name.'.detachiev_id');
		$db->join('prestasi_pelanggaran', 'prestasi_pelanggaran.achiev_id = prestasi_pelanggaran_detil.achiev_id');
		$db->join('reward_punishment', 'reward_punishment.reward_id = '.$this->table_name.'.reward_id');
    	if ($orderby) $this->db->order_by($orderby);
    	return $query = $this->db->get_where($this->table_name, array('nilai_id' => $id));
    }
    
    function select_range($detachiev_id, $min, $max){
    	$db = $this->db;
    	$db->select('*');
		$db->join('prestasi_pelanggaran_detil', 'prestasi_pelanggaran_detil.detachiev_id = '.$this->table_name.'.detachiev_id');
		$db->join('prestasi_pelanggaran', 'prestasi_pelanggaran.achiev_id = prestasi_pelanggaran_detil.achiev_id');
		$db->join('reward_punishment', 'reward_punishment.reward_id = '.$this->table_name.'.reward_id');
		
		$db->where($this->table_name.".detachiev_id=".$db->escape($detachiev_id));
		$db->where("nilai_min BETWEEN $min AND $max");
		$db->or_where("nilai_max BETWEEN $min AND $max");
    	return $query = $db->get($this->table_name);
    }
    
    function check_range($detachiev_id, $value, $nilai_id=false){
    	$db = $this->db;
    	$db->select('*');
		$db->where($this->table_name.".detachiev_id=".$db->escape($detachiev_id));
		$db->where("'$value' BETWEEN nilai_min AND nilai_max");
		if ($nilai_id) $db->where("nilai_id != ".$db->escape($nilai_id));
    	return $query = $db->get($this->table_name);
    }
}